﻿using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.Xml;
using DocumentFormat.OpenXml.Lite.Helper;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Texts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using DocumentFormat.OpenXml.Lite.Core;
using DocumentFormat.OpenXml.Lite.Core.Interfaces;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Base
{
    [XmlRoot("r", Namespace = Constants.WordMain)]
    public class Run : XmlSerializable, IOfficeElement
    {
        [XmlIgnore]
        public Guid DocumentId { get; set; }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        [Optional]
        [XmlElement("rPr", Namespace = Constants.WordMain)]
        public RunProperties Properties { get; set; }

        [Optional]
        [XmlElement("", Namespace = Constants.WordMain)]
        public XmlList<IRunElement> Elements { get; set; }

        public Run()
        {
            Properties = new RunProperties();

            Elements = new XmlList<IRunElement>();
            Elements.Changing += Elements_Changing;
        }

        void Elements_Changing(object sender, ListEventArgs<IRunElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType() == typeof(IRunSingleElement))
                {
                    var element = Elements.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + Elements.ToString());
                    }
                }
            }
        }

        public void AddSimpleText(string text)
        {
            AddElement(new Text(text));
        }

        public void AddElement(IRunElement element)
        {
            Elements.Add(element);
        }
        
        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddElement(IRunSingleElement element)
        {
            var obj = Elements.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as IRunSingleElement).Copy(element);
            }
            else
            {
                Elements.Add(element);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddProperty(IRunPropElement element)
        {
            if (Properties == null)
            {
                Properties = new RunProperties();
            }

            var obj = Properties.Properties.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as IRunPropElement).Copy(element);
            }
            else
            {
                Properties.Properties.Add(element);
                return true;
            }

            return false;
        }
    }

    [XmlRoot("rPr", Namespace = Constants.WordMain)]
    public class RunProperties : XmlSerializable
    {
        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        [Optional]
        [XmlElement("", Namespace = Constants.WordMain)]
        public XmlList<IRunPropElement> Properties { get; set; }

        public RunProperties()
        {
            Properties = new XmlList<IRunPropElement>();
            Properties.Changing += Properties_Changing;
        }

        void Properties_Changing(object sender, ListEventArgs<IRunPropElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType().IsInheritedFrom(typeof(IRunPropElement)))
                {
                    var element = Properties.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + Properties.ToString());
                    }
                }
            }
        }
    }
}
